package javax.net.p2p.bio.server;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.p2p.bio.handler.ChatServerHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/****************************************************
 * @description 
 * @author   karl
 * @version  1.0, 2018-11-3
 * @see HISTORY
 *      Date        Desc          Author      Operation
 *  	2018-11-3   创建文件       karl        create
 * @since 2017 Phyrose Science & Technology (Kunming) Co., Ltd.
 **************************************************/
public class ChatServer {
    private static final Log log = LogFactory.getLog(ChatServer.class);
    public static final int SERVER_PORT = 9876;

    public static void main(String[] args) throws Exception {
        //独立线程循环处理聊天事件：
        final boolean f = true;

        Thread t = new Thread(() -> {
            while (f) {
                try {
                    Thread.sleep(500l);
                } catch (InterruptedException ex) {
                    log.error(ex);
                }
                ChatServerHandler.processChatEvents();
            }

        });
        t.start();

        //开启聊天服务器监听
        ServerSocket server = new ServerSocket(SERVER_PORT);
        System.out.println("聊天服务器已启动，等待客户端连接，端口：" + SERVER_PORT);
        while (f) {
            Socket socket = server.accept();//获得传入的连接
            new Thread(new ChatServerHandler(socket)).start();
        }
    }

}
